package controller;

import dal.BookDAL;
import entity.Book;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author NhatNK
 */
@ManagedBean
@SessionScoped
public class BookController {
    private DataModel<Book> books;
    private BookDAL bookDal;
    private Book currentBook;
    public BookController() {
        bookDal = new BookDAL();
    }
    private void init(){
        books = new ListDataModel<Book>(bookDal.loadBooks());
    }

    public DataModel<Book> getBooks() {
        init();
        return books;
    }

    public void setBooks(DataModel<Book> books) {
        this.books = books;
    }

    public Book getCurrentBook() {
        return currentBook;
    }

    public void setCurrentBook(Book currentBook) {
        this.currentBook = currentBook;
    }
    
    public String create(){
        currentBook = new Book();
        return "create";
    }
    public String doCreate(){
        List<Book> list = bookDal.loadBooks();
        list.add(currentBook);
        bookDal.saveBooks(list);
        return "index";
    }
}
